//
//  UIViewControllerExt.swift
//  WNLockDemo
//
//  Created by 裴威 on 31/08/2018.
//  Copyright © 2018 wunu. All rights reserved.
//

import UIKit

extension UIViewController {
    func confiureNavigationBar(bgColor: UIColor = Colors.white) {
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.barTintColor = bgColor
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.view.backgroundColor = Colors.background
    }
    func configureNavigationBarBack() {
        let backButton = UIButton(type: .custom)
        backButton.setImage(UIImage(named: "navi_back"), for: .normal)
        backButton.sizeToFit()
        backButton.addTarget(self, action: #selector(handleNavigationBarGoBack(_:)), for: .touchUpInside)
        let backBarButtonItem = UIBarButtonItem(customView: backButton)
        navigationItem.leftBarButtonItem = backBarButtonItem
        
    }
    @objc func handleNavigationBarGoBack(_ sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
}

extension UIViewController {
    func showEmptyView(message: String) {
        let bgView = UIView(frame: self.view.bounds)
        bgView.backgroundColor = Colors.white
        self.view.addSubview(bgView)
        let imageView = UIImageView(image: UIImage(named: "no_data_alert"))
        imageView.sizeToFit()
        imageView.frame = CGRect(x: (bgView.bounds.width - imageView.frame.width) / 2.0, y: 10, width: imageView.frame.width, height: imageView.frame.height)
        bgView.addSubview(imageView)
        let alertLabel = UILabel(frame: .zero)
        alertLabel.text = message
        alertLabel.font = Fonts.regular(ofSize: 15)
        alertLabel.textColor = Colors.title
        alertLabel.sizeToFit()
        alertLabel.frame = CGRect(x: (bgView.bounds.width - alertLabel.bounds.width) / 2.0, y: imageView.frame.maxY + 10, width: alertLabel.frame.width, height: alertLabel.frame.height)
        bgView.addSubview(alertLabel)
    }
    func hideEmptyView() {
        for v in self.view.subviews {
            if v is _EmptyViewBackgroundView {
                v.removeFromSuperview()
            }
        }
    }
    fileprivate class _EmptyViewBackgroundView: UIView {}
}
